package service

import (
	"kubernate-server/module"

	"gorm.io/gorm"
)

/**
 * Author: sbw
 * Description: clusterAuth dao
 * Date:20250927
 */
type ClusterDao struct{}

func (a *ClusterDao) IsExist(clusterName string) (bool, error) {
	var clusterAuth module.ClusterAuth
	err := GetCmsApp().db.Where("cluster_name = ? and is_delete = '0' ", clusterName).First(&clusterAuth).Error
	if err == gorm.ErrRecordNotFound {
		return false, nil
	}

	if err != nil {
		return false, err
	}
	return true, nil
}

func (a *ClusterDao) InsertCluster(clusterAuth module.ClusterAuth) error {
	if err := GetCmsApp().db.Create(&clusterAuth).Error; err != nil {
		return err
	}
	return nil
}

func (a *ClusterDao) QueryClusters() []module.ClusterSimpleResult {
	var clusters []module.ClusterSimpleResult
	GetCmsApp().db.Where("is_delete = '0' ").Find(&clusters)
	return clusters
}

func (a *ClusterDao) DeleteCluster(id int64) error {
	var clusterAuth module.ClusterAuth
	err := GetCmsApp().db.Model(clusterAuth).Where(" id = ? ", id).Update("is_delete", 1).Error
	return err
}
